package com.fushenlan.commons;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @author PC
 * @title: GeneralResponse
 * @projectName SpringBootDemo
 * @description: TODO
 * @date 2022/1/21 11:15
 */
@ApiModel(
        description = "响应报文（统一规范的返回类）"
)
@JsonInclude(JsonInclude.Include.ALWAYS)
@JsonPropertyOrder({"code", "msg", "desc", "data"})
@Data
public class GeneralResponse<T> implements Serializable {
    private static final long serialVersionUID = 2227587489873589438L;
    public static final String CODE_SUCCESS = "0000";
    @ApiModelProperty(
            position = 1,
            value = "响应状态通用错误码",
            notes = "响应状态通用错误码",
            required = true
    )
    private String code = "0000";
    @ApiModelProperty(
            position = 2,
            value = "响应错误信息",
            notes = "响应错误信息"
    )
    private String msg;
    @ApiModelProperty(
            position = 3,
            value = "响应状态通用错误码",
            notes = "响应状态通用错误码"
    )
    private String desc;
    @ApiModelProperty(
            position = 10,
            value = "执行成功后返回结果",
            notes = "响应错误友好描述信息"
    )
    private T data;

    public GeneralResponse() {

    }

    public GeneralResponse(T data) {
        this.data = data;
    }

    public GeneralResponse(String code, String msg, String desc) {
        this.code = code;
        this.msg = msg;
        this.desc = desc;
    }

    public static <T> GeneralResponse<T> success() {
        return new GeneralResponse("0000", "Success", "成功");
    }

    public static <T> GeneralResponse<T> success(T data) {
        return new GeneralResponse(data);
    }

    public static <T> GeneralResponse<T> process(String msg, String desc) {
        return new GeneralResponse("5555", msg, desc);
    }

    public static <T> GeneralResponse<T> fail(String code, String msg, String desc) {
        return new GeneralResponse(code, msg, desc);
    }
    public static <T> GeneralResponse<T> fail() {
        return new GeneralResponse("-1", "系统内部错误","");
    }

    public static <T> GeneralResponse<T> fail(ErrorStatus status) {
        return new GeneralResponse(status.getCode(), status.getMsg(), status.getHmsg());
    }

    public static <T> GeneralResponse<T> fail(ErrorStatus status, String msg) {
        return new GeneralResponse(status.getCode(), msg, status.getHmsg());
    }

    public static <T> GeneralResponse<T> fail(ErrorStatus status, String msg, String hmsg) {
        return new GeneralResponse(status.getCode(), msg, hmsg);
    }

    public static <T> GeneralResponse<T> failWithPrams(String code, String msg, String desc, Object... descArgs) {
        return new GeneralResponse(code, msg, String.format(desc, descArgs));
    }

    public static <T> GeneralResponse<T> fail(KVDEnum kvdEnum) {
        return new GeneralResponse(kvdEnum.getKey(), kvdEnum.getValue(), kvdEnum.getDesc());
    }

    public static <T> GeneralResponse<T> fail(KVDEnum kvdEnum, String desc) {
        return new GeneralResponse(kvdEnum.getKey(), kvdEnum.getValue(), desc);
    }

    public static <T> GeneralResponse<T> fail9898(String errorMsg) {
        GeneralResponse<T> rsp = fail(ApiGlobalErrorDefinition.ERROR_RSP);
        rsp.setMsg(errorMsg);
        return rsp;
    }
}
